<template>
<div class="post-comment">
    <van-field class="post-field" v-model.trim="message" rows="2" autosize type="textarea" maxlength="50" placeholder="优质评论将会被优先展示" show-word-limit />
    <van-button type="primary" size="small" @click="onPost" :disabled="message.length===0">发布</van-button>
</div>
</template>

<script>
import { addComment } from '@/api/comment'
export default {
  name: 'PostComment',
  components: {},
  props: {
    target: {
      type: [Number, String, Object],
      required: true
    }
  },
  inject: ['art_id'],
  data () {
    return {
      message: ''
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onPost () {
      // 成功之前的等待loading
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0// 不会消失
      })
      try {
        const { data: res } = await addComment({
          target: this.target.toString(),
          content: this.message,
          art_id: this.art_id ? this.art_id.toString() : null
        })
        this.$toast.success('添加评论成功')
        console.log(res)
        this.$emit('update-post', res.data.new_obj)
        this.message = ''
      } catch (err) {
        this.$toast.fail('添加评论失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.post-comment {
    display: flex;
    padding: 15px;
    align-items: flex-end;

    .post-field {
        background: #f5f7f9;
        margin-right: 15px;
    }
}
</style>
